import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_sure_sheet.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/widget_extension.dart';
import 'package:safetymonitor/generated/assets.dart';
import 'select_people_controller.dart';

/// 选择人员页面 todo 不灵活待优化
class DutySelectPeoplePage extends StatelessWidget {
  DutySelectPeoplePage({super.key});

  final controller = Get.put(SelectPeopleController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return PopScope(
          canPop: controller.canPop,
          onPopInvoked: (bool didPop) {
            // 不能退出时执行的操作
            if (!didPop) {
              controller.exitJudgment();
            }
          },
          child: Scaffold(
            backgroundColor: const Color(0xFFF7FBFE),
            appBar: CustomAppBar(
              titleText: '人员选择',
            ),
            body: Container(
              padding: EdgeInsets.only(right: 20.w, left: 20.w, bottom: 20.w),
              child: Column(
                children: [
                  controller.selectedNode.isEmpty ? AppGaps.vGap10 : _currentNodeTitle(),
                  AppGaps.vGap2,
                  Expanded(child: _organizationCard()),
                  AppGaps.vGap60,
                ],
              ),
            ),
            bottomSheet: CustomSureSheet(onTap: controller.confirm),
          ),
        );
      },
    );
  }

  /// 当前所在节点名称
  Widget _currentNodeTitle() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 14.w),
      alignment: Alignment.centerLeft,
      height: 40.w,
      decoration: BoxDecoration(
        color: const Color(0xFFFFFFFF),
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(8.r),
          topRight: Radius.circular(8.r),
        ),
      ),
      child: ListView.separated(
          shrinkWrap: true,
          // 设置为true，
          scrollDirection: Axis.horizontal,
          // 设置滚动方向为水平方向
          itemBuilder: (context, index) {
            return Container(
              alignment: Alignment.center,
              child: Text(
                controller.selectedNode[index],
                style: TextStyle(
                  color: controller.selectedNode.length != index + 1 ? const Color(0xFFA5BCDD) : const Color(0xFF2267C0),
                  fontSize: 13.sp,
                ),
              ),
            ).clickOnce(() {
              controller.onClickNode(index);
            });
          },
          separatorBuilder: (context, index) {
            return SvgPicture.asset(
              Assets.svgIconMeCenterRight,
              width: 16.w,
              height: 16.w,
            ).marginSymmetric(horizontal: 6.w);
          },
          itemCount: controller.selectedNode.length),
    );
  }

  /// 组织架构样式
  Widget _organizationCard() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 14.w),
      alignment: Alignment.topCenter,
      decoration: BoxDecoration(
        color: const Color(0xFFFFFFFF),
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(8.r),
          bottomRight: Radius.circular(8.r),
        ),
      ),
      child: SingleChildScrollView(
        child: Column(
          children: [
            // 部门样式
            _getDepartmentView(),
            // 分割线
            Visibility(
              visible: controller.department.isNotEmpty,
              child: Divider(height: 0.5.w, color: const Color(0xFFEAF2FA)),
            ),
            // 人员样式
            _getPersonView(),
            Visibility(
              visible: controller.personList.isEmpty && controller.department.isEmpty,
              child: _getNoData(),
            )
          ],
        ),
      ),
    );
  }

  /// 部门样式
  Widget _getDepartmentView() {
    return ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (context, index) {
          return Container(
            margin: EdgeInsets.symmetric(vertical: 20.w),
            child: Row(
              children: [
                SvgPicture.asset(
                  Assets.svgIconOrganize,
                  width: 30.w,
                  height: 30.w,
                ),
                AppGaps.hGap12,
                Expanded(
                  child: Text(
                    controller.department[index].getShowName(),
                    style: TextStyle(
                      color: const Color(0xFF071D56),
                      height: 1.2,
                      fontWeight: FontWeight.w400,
                      fontSize: 15.sp,
                    ),
                  ),
                ),
                SvgPicture.asset(
                  Assets.svgIconMeCenterRight,
                  width: 16.w,
                  height: 16.w,
                )
              ],
            ),
          ).clickOnce(() {
            controller.onClickDepartment(index);
          });
        },
        separatorBuilder: (context, index) {
          return Divider(height: 0.5.w, color: const Color(0xFFEAF2FA));
        },
        itemCount: controller.department.length);
  }

  /// 人员样式
  Widget _getPersonView() {
    return ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (context, index) {
          return Container(
            margin: EdgeInsets.symmetric(vertical: 20.w),
            child: Row(
              children: [
                SvgPicture.asset(
                  Assets.svgIconAddressPerson,
                  width: 30.w,
                  height: 30.w,
                ),
                AppGaps.hGap12,
                Expanded(
                  child: Text(
                    controller.personList[index].nickname,
                    style: TextStyle(
                      color: const Color(0xFF071D56),
                      height: 1.2,
                      fontWeight: FontWeight.w400,
                      fontSize: 15.sp,
                    ),
                  ),
                ),
                SizedBox(
                  width: 80.w,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      SvgPicture.asset(
                        controller.personList[index].getIsChecked() ? Assets.svgIconSelect : Assets.svgIconUnSelect,
                        width: 16.w,
                        height: 16.w,
                      ),
                      AppGaps.hGap8,
                      Text(
                        '选择',
                        style: TextStyle(
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w400,
                          color: const Color(0xff5C6DFF),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ).clickOnce(() {
            controller.onClickPerson(index);
          });
        },
        separatorBuilder: (context, index) {
          return Divider(height: 0.5.w, color: const Color(0xFFEAF2FA));
        },
        itemCount: controller.personList.length);
  }

  /// 暂无数据
  Widget _getNoData() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        AppGaps.vGap150,
        SvgPicture.asset(
          Assets.svgIconNoPerson,
        ),
        AppGaps.vGap20,
        Text(
          '暂无成员',
          style: TextStyle(
            color: const Color(0xFFA5BCDD),
            fontSize: 14.sp,
          ),
        ),
      ],
    );
  }
}
